import java.util.Arrays;
import java.util.Comparator;

class Student implements Comparable<Student> {
    public int age;
    public String name;

    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
    @Override
    public int compareTo(Student o) {//自定义，用姓名排序
        if(o.name==null){//判断是否为空
            return 0;
        }
        if(this.name.compareTo(o.name)>1){
            return 1;
        } else if (this.name.compareTo(o.name)<1) {
            return -1;
        }else {
            return 0;
        }
    }
    /* @Override
    public int compareTo(Student o) {//自定义，用年龄排序
        if(o.age<0){//判断输入的数据是否合理
            return -1;
        }
        if(this.age>o.age){
            return 1;
        } else if (this.age<o.age) {
            return -1;
        }else{
        return 0;
        }
    }*/
}
/*class NameComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}*/
class AgeComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age-o2.age;
    }
}
public class Main {
    public static void main(String[] args) {
        Student[] Student = new Student[3];
        Student[0] = new Student(13, "xxxx");
        Student[1] = new Student(-12, "xx");
        Student[2] = new Student(1, "xxx");
        /*NameComparator nameComparator=new NameComparator();*/
        AgeComparator ageComparator=new AgeComparator();
        Arrays.sort(Student,ageComparator);
        System.out.println(Arrays.toString(Student));//重写toString
    }
}